package com.aboo.action;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.aboo.po.AdminUserPO;
import com.aboo.po.FrameConstantPO;
import com.aboo.po.Page;
import com.aboo.po.RolePO;
import com.aboo.pub.constant.Constant;
import com.aboo.pub.constant.SessionConstant;
import com.aboo.pub.exception.BasicException;
import com.aboo.pub.token.AdminLoginToken;
import com.aboo.service.AdminUserService;
import com.aboo.service.FrameConstantService;
import com.aboo.service.RoleService;
import com.aboo.util.DateUtil;
import com.aboo.util.EncryptionUtil;
import com.aboo.util.RequestUtil;
import com.aboo.util.SessionUtil;

public class AdminUserAction extends BaseAction {

	private static final long serialVersionUID = -4610357825309813250L;
	private AdminUserService adminUserService;
	private FrameConstantService frameConstantService;
	private RoleService roleService;
	private AdminUserPO adminUser = new AdminUserPO();
	
	public String list() throws BasicException {
		Integer currentPageNO = RequestUtil.getParameterIntegerDefault(request, Page.CURRENT_PAGE_NO, Page.DEFAULT_CURRENT_PAGE_NO);
		Integer pageSize = RequestUtil.getParameterIntegerDefault(request, Page.PAGE_SIZE, Page.DEFAULT_PAGE_SIZE);
		Integer sectionSize = RequestUtil.getParameterIntegerDefault(request, Page.SECTION_SIZE, Page.DEFAULT_SECTION_SIZE);
		
		page = new Page(currentPageNO, pageSize, sectionSize);
		String tmpLoginName = null;
		if (StringUtils.isNotEmpty(adminUser.getLoginName())) {
			tmpLoginName = adminUser.getLoginName();
			adminUser.addCondition("LOGIN_NAME", Constant.CONDITION_LIKE, "%" + tmpLoginName + "%");
			adminUser.setLoginName("");
		}
		adminUserService.findUserList(ri, adminUser, page);
		List<AdminUserPO> result = (List<AdminUserPO>) ri.getObj();
		
		FrameConstantPO constant = new FrameConstantPO();
		constant.setModuleName("USER_TYPE");
		frameConstantService.findFrameConstantList(ri, constant, null);
		List<FrameConstantPO> memberTypeList = (List<FrameConstantPO>) ri.getObj();
		
		if (StringUtils.isNotEmpty(tmpLoginName)) {
			adminUser.setLoginName(tmpLoginName);
		}
		RequestUtil.setAttribute(request, "userList", transform(result, memberTypeList));
		RequestUtil.setAttribute(request, "memberTypeList", memberTypeList);
		return BaseAction.LIST;
	}
	
	public String create() throws BasicException {
		FrameConstantPO constant = new FrameConstantPO();
		constant.setModuleName("USER_TYPE");
		frameConstantService.findFrameConstantList(ri, constant, null);
		List<FrameConstantPO> memberTypeList = (List<FrameConstantPO>) ri.getObj();
		
		RequestUtil.setAttribute(request, "memberTypeList", memberTypeList);
		return BaseAction.CREATE;
	}
	
	public String createExc() throws BasicException {
		String loginName = adminUser.getLoginName();
		AdminUserPO param = new AdminUserPO();
		param.setLoginName(loginName);
		adminUserService.findUserCount(ri, param);
		Integer count = (Integer)ri.getObj();
		if (count != null && count.intValue() > 0) {
			throw new BasicException("存在相同登陆名用户，无法创建！");
		}
		
		adminUserService.saveAdminUser(ri, adminUser);
		return BaseAction.CREATE_EXC;
	}
	
	public String delete() throws BasicException {
		String checkedIds = RequestUtil.getParameterStringDefault(request, "checkedIds", "-1");
		List<String> userIdList = null;
		if (!StringUtils.isEmpty(checkedIds)) {
			String[] userIds = checkedIds.split(",");
			userIdList = Arrays.asList(userIds);
		}
		adminUserService.deleteAdminUser(ri, userIdList);
		return BaseAction.DELETE;
	}
	
	public String modify() throws BasicException {
		String checkedIds = RequestUtil.getParameterStringDefault(request, "checkedIds", "-1");
		adminUser = new AdminUserPO();
		adminUser.setId(new Integer(checkedIds));
		adminUserService.findUser(ri, adminUser);
		adminUser = (AdminUserPO) ri.getObj();
		
		FrameConstantPO constant = new FrameConstantPO();
		constant.setModuleName("USER_TYPE");
		frameConstantService.findFrameConstantList(ri, constant, null);
		List<FrameConstantPO> memberTypeList = (List<FrameConstantPO>) ri.getObj();
		
		RequestUtil.setAttribute(request, "memberTypeList", memberTypeList);
		return BaseAction.MODIFY;
	}
	
	public String modifyExc() throws BasicException {
		adminUserService.updateAdminUser(ri, adminUser);
		return BaseAction.MODIFY_EXC;
	}
	
	public String modifySelfPwd() throws BasicException {
		AdminLoginToken adminLoginToken = (AdminLoginToken) SessionUtil.getObject(request, SessionConstant.ADMIN_LOGIN_TOKEN);
		adminUser = adminLoginToken.getAdminUserInfo();
		return "modifySelfPwd";
	}
	
	public String modifySelfPwdExc() throws BasicException {
		adminUserService.findUser(ri, adminUser);
		AdminUserPO tmp = (AdminUserPO)ri.getObj();
		if (tmp == null || !tmp.getPassword().equals(EncryptionUtil.encodeMD5(adminUser.getPassword()))) {
			throw new BasicException("原密码输入错误！");
		}
		
		String newPassword = RequestUtil.getParameterString(request, "newPassword");
		adminUser.setPassword(newPassword);
		adminUserService.updateAdminUser(ri, adminUser);
		return "modifySelfPwdExc";
	}
	
	public String view() throws BasicException {
		String checkedIds = RequestUtil.getParameterStringDefault(request, "checkedIds", "-1");
		adminUser = new AdminUserPO();
		adminUser.setId(new Integer(checkedIds));
		adminUserService.findUser(ri, adminUser);
		adminUser = (AdminUserPO) ri.getObj();
		
		FrameConstantPO constant = new FrameConstantPO();
		constant.setModuleName("USER_TYPE");
		frameConstantService.findFrameConstantList(ri, constant, null);
		List<FrameConstantPO> memberTypeList = (List<FrameConstantPO>) ri.getObj();
		
		RequestUtil.setAttribute(request, "memberTypeList", memberTypeList);
		return BaseAction.VIEW;
	}
	
	public String modifyPwd() throws BasicException {
		String checkedIds = RequestUtil.getParameterStringDefault(request, "checkedIds", "-1");
		adminUser = new AdminUserPO();
		adminUser.setId(new Integer(checkedIds));
		adminUserService.findUser(ri, adminUser);
		adminUser = (AdminUserPO) ri.getObj();
		
		return "modifyPwd";
	}
	
	public String editUserRole() throws BasicException {
		String checkedId = RequestUtil.getParameterStringDefault(request, "checkedIds", "");
		adminUser.setId(new Integer(checkedId));
		adminUserService.findUser(ri, adminUser);
		adminUser = (AdminUserPO) ri.getObj();
		
		roleService.findRoleList(ri, new RolePO(), null);
		List<RolePO> allRoleList = (List<RolePO>) ri.getObj();
		
		adminUserService.findUserRoleList(ri, adminUser.getId());
		List<RolePO> myRoleList = (List<RolePO>) ri.getObj();
		
		RequestUtil.setAttribute(request, "allRoleList", allRoleList);
		RequestUtil.setAttribute(request, "myRoleIds", getRoleIds(myRoleList));
		return "editUserRole";
	}
	
	public String editUserRoleExc() throws BasicException {
		String checkedRoleIds = RequestUtil.getParameterStringDefault(request, "checkedRoleIds", "");
		if (!StringUtils.isEmpty(checkedRoleIds)) {
			String[] checkedIdArr = checkedRoleIds.split(",");
			List<String> checkedIdList = Arrays.asList(checkedIdArr);
			
			adminUserService.saveUserRoleList(ri, adminUser.getId(), checkedIdList);
		} else {
			adminUserService.saveUserRoleList(ri, adminUser.getId(), null);
		}
		
		return "editUserRoleExc";
	}
	
	private List<AdminUserPO> transform(List<AdminUserPO> userList, List<FrameConstantPO> memberTypeList) {
		if (userList != null) {
			for (AdminUserPO user : userList) {
				if (memberTypeList != null) {
					for (FrameConstantPO constant : memberTypeList) {
						if (constant.getConstantValue().equals(user.getMemberType())) {
							user.setMemberType(constant.getConstantName());
							break;
						}
					}
				}
				if (user.getLastSigninTime() != null) {
					user.setLastSigninTime(DateUtil.getDateTime(DateUtil.getDate(user.getLastSigninTime(), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
				}
			}
		}
		return userList;
	}
	
	private String getRoleIds(List<RolePO> roleList) {
		StringBuffer roleIdsBuf = new StringBuffer("");
		if (roleList != null) {
			for (RolePO role : roleList) {
				roleIdsBuf.append(role.getId().toString() + ",");
			}
			if (!StringUtils.isEmpty(roleIdsBuf)) {
				return roleIdsBuf.substring(0, roleIdsBuf.length() - 1);
			}
		}
		return "";
	}
	
	public String modifyPwdExc() throws BasicException {
		adminUserService.updateAdminUser(ri, adminUser);		
		return "modifyPwdExc";
	}

	public AdminUserService getAdminUserService() {
		return adminUserService;
	}
	public void setAdminUserService(AdminUserService adminUserService) {
		this.adminUserService = adminUserService;
	}
	public FrameConstantService getFrameConstantService() {
		return frameConstantService;
	}
	public void setFrameConstantService(FrameConstantService frameConstantService) {
		this.frameConstantService = frameConstantService;
	}
	public AdminUserPO getAdminUser() {
		return adminUser;
	}
	public void setAdminUser(AdminUserPO adminUser) {
		this.adminUser = adminUser;
	}
	public RoleService getRoleService() {
		return roleService;
	}
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
}
